﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using Kinlo.MesCheck;
using Kinlo.SWDLib;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// BatchInputView.xaml 的交互逻辑
    /// </summary>
    public partial class BatchInputView : Window
    {
        public BatchInputView()
        {
            InitializeComponent();
            Load();
        }
        void Load()
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings.AllKeys.Contains("BatchNo"))
                {
                    txtBatchNo.Text = config.AppSettings.Settings["BatchNo"].Value;
                }
                if (config.AppSettings.Settings.AllKeys.Contains("BatchNo1"))
                {
                    txtBatchNo1.Text = config.AppSettings.Settings["BatchNo1"].Value;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("加载数据发生错", "错误");
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtBatchNo.Text) || string.IsNullOrEmpty(txtBatchNo1.Text))
                {
                    MessageBox.Show("料号或批次号不能空，请重新录入", "错误");
                    return;
                }
                else
                {
                    if (GlobalValues.MesOnOff)
                    {
                      var batch= txtBatchNo.Text.Trim();
                        var batch1 = txtBatchNo1.Text.Trim();

                        var r = Lib.ItemsGroupCheck(GlobalValues.MoNumber, GlobalValues.EquNo, batch, batch1);

                        //var r = MESCheck.CellBOMItemsCheck(scanResult, GlobalValues.MoNumber, GlobalValues.EmpNo, GlobalValues.EquNo, ItemBatchNo);
                        Logger.Info(string.Format("电解液校验 {0},{1},{2},{3} 结果：{4}", GlobalValues.MoNumber, GlobalValues.EquNo, batch, batch1, r));

                        if (r.Contains("TRUE"))
                        {
                            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                            if (config.AppSettings.Settings.AllKeys.Contains("BatchNo"))
                            {
                                config.AppSettings.Settings["BatchNo"].Value = txtBatchNo.Text.Trim();
                            }
                            else
                            {
                                config.AppSettings.Settings.Add("BatchNo", txtBatchNo.Text.Trim());
                            }
                            if (config.AppSettings.Settings.AllKeys.Contains("BatchNo1"))
                            {
                                config.AppSettings.Settings["BatchNo1"].Value = txtBatchNo1.Text.Trim();
                            }
                            else
                            {
                                config.AppSettings.Settings.Add("BatchNo1", txtBatchNo1.Text.Trim());
                            }

                            config.Save(ConfigurationSaveMode.Modified);
                            ConfigurationManager.RefreshSection("appSettings");

                            GlobalValues.BatchNo = batch;
                            GlobalValues.BatchNo1 = batch1;
                            Messenger.Default.Send<string>("", "CmdUpdateBatchNo");
                            MessageBox.Show("录入成功 ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("电解液MES校验失败！ ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    else
                    {
                        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                        if (config.AppSettings.Settings.AllKeys.Contains("BatchNo"))
                        {
                            config.AppSettings.Settings["BatchNo"].Value = txtBatchNo.Text.Trim();
                        }
                        else
                        {
                            config.AppSettings.Settings.Add("BatchNo", txtBatchNo.Text.Trim());
                        }
                        if (config.AppSettings.Settings.AllKeys.Contains("BatchNo1"))
                        {
                            config.AppSettings.Settings["BatchNo1"].Value = txtBatchNo1.Text.Trim();
                        }
                        else
                        {
                            config.AppSettings.Settings.Add("BatchNo1", txtBatchNo1.Text.Trim());
                        }
                        config.Save(ConfigurationSaveMode.Modified);
                        ConfigurationManager.RefreshSection("appSettings");
                        GlobalValues.BatchNo = txtBatchNo.Text.Trim();
                        GlobalValues.BatchNo1 = txtBatchNo1.Text.Trim();
                        Logger.Info(string.Format("未启用MES 料号:{0} 批次号:{1} ", GlobalValues.BatchNo, GlobalValues.BatchNo1));
                        Messenger.Default.Send<string>("", "CmdUpdateBatchNo");
                        MessageBox.Show("录入成功 ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                var msg = String.Format("电解液校验发生异常 {0}", ex.Message);
                MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();

        }
    }
}
